package com.shanzmoo.core.config;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 微信公众号配置属性类
 *
 * @author fs
 */
@Data
@Component
@ConfigurationProperties(prefix = "wx.mp")
@RequiredArgsConstructor
public class WxMpProperties {
    private final RedisTemplate redisTemplate;
    /**
     * 设置微信公众号的appid
     */
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    private String secret;

    /**
     * 设置微信公众号的token
     */
    private String token;

    /**
     * 设置微信公众号的EncodingAESKey
     */
    private String aesKey;

    @Bean
    public WxMpService wxMpService() {
        WxMpService service = new WxMpServiceImpl();
        WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage(redisTemplate);
        configStorage.setAppId(this.appId);
        configStorage.setSecret(this.secret);
        configStorage.setToken(this.token);
        configStorage.setAesKey(this.aesKey);
        service.setWxMpConfigStorage(configStorage);
        return service;
    }
}
